﻿using System.Collections.Generic;
using Cms.Application.WebFramework.Presenters;
using Cms.Domain;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace Cms.Tests.Unit.Application.WebFramework.Presenters
{
    [TestClass]
    public class RequestDetailsPresenterTests : BasePresenterTests
    {       
        private RequestDetailsPresenter _presenter;
        private Site _domainA = new Site();
        private Site _domainB = new Site();
        private Locale _en;
        private Locale _sv;
        
        private void GeneralPrepare()
        {
            InitRepositoryFactoryMock();
            _presenter = new RequestDetailsPresenter(FactoryMock.Object);
        }
        
        [TestMethod]
        public void IsSystemPageRequest_Yes_ReturnTrue()
        {
            GeneralPrepare();
            Assert.IsTrue(_presenter.IsSystemPageRequest("admin/"));
            Assert.IsTrue(_presenter.IsSystemPageRequest("admin/default.aspx"));
            Assert.IsTrue(_presenter.IsSystemPageRequest("admin/mediadefault.aspx"));
            Assert.IsTrue(_presenter.IsSystemPageRequest("install.aspx"));
            Assert.IsTrue(_presenter.IsSystemPageRequest("webresource.axd"));
            Assert.IsTrue(_presenter.IsSystemPageRequest("scriptresource.axd"));
            Assert.IsTrue(_presenter.IsSystemPageRequest("authentication.asmx/jsdebug"));
        }

        [TestMethod]
        public void IsSystemPageRequest_No_ReturnFalse()
        {
            GeneralPrepare();
            Assert.IsFalse(_presenter.IsSystemPageRequest("administrativetools/"));
            Assert.IsFalse(_presenter.IsSystemPageRequest("default.aspx"));
            Assert.IsFalse(_presenter.IsSystemPageRequest("vehicles/cars/default.aspx"));
        }

        [TestMethod]
        public void IsSitePreview_Yes_True()
        {
            GeneralPrepare();
            Assert.IsTrue(_presenter.IsSitePreview("Admin/SitePreview.aspx"));
            Assert.IsTrue(_presenter.IsSitePreview("/applicationpagepreview.aspx"));
            Assert.IsTrue(_presenter.IsSitePreview("admin/contentpreview.aspx"));
        }
        
        [TestMethod]
        public void IsSitePreview_No_False()
        {
            GeneralPrepare();
            Assert.IsFalse(_presenter.IsSitePreview("Admin/xx.aspx"));
            Assert.IsFalse(_presenter.IsSitePreview("helloworld.aspx"));
        }
        
        [TestMethod]
        public void SingleSignOnAuthorizationRequired_Yes_ReturnTrue()
        {
            GeneralPrepare();
            Assert.IsTrue(_presenter.SingleSignOnAuthorizationRequired("Admin/Default.aspx"));
            Assert.IsTrue(_presenter.SingleSignOnAuthorizationRequired("admin/Explorer.aspx"));
            Assert.IsTrue(_presenter.SingleSignOnAuthorizationRequired("ssoauth.ashx"));
        }
        [TestMethod]
        public void SingleSignOnAuthorizationRequired_No_ReturnFalse()
        {
            GeneralPrepare();
            Assert.IsFalse(_presenter.SingleSignOnAuthorizationRequired("Admin/edit_area/edit_area.js"));
            Assert.IsFalse(_presenter.SingleSignOnAuthorizationRequired("Admin/tiny_mce/tiny_mce.js"));
            Assert.IsFalse(_presenter.SingleSignOnAuthorizationRequired("Admin/tiny_mce/plugins/frediumplugins/editor_plugin.js"));
            Assert.IsFalse(_presenter.SingleSignOnAuthorizationRequired("Admin/tiny_mce/plugins/frediumplugins/frediumplugins.aspx"));
            Assert.IsFalse(_presenter.SingleSignOnAuthorizationRequired("Admin/css/main.css"));
            Assert.IsFalse(_presenter.SingleSignOnAuthorizationRequired("Admin/img/logo.png"));
            Assert.IsFalse(_presenter.SingleSignOnAuthorizationRequired("Admin/js/CmsLib.js"));
            Assert.IsFalse(_presenter.SingleSignOnAuthorizationRequired("Admin/Ping.aspx"));
            Assert.IsFalse(_presenter.SingleSignOnAuthorizationRequired("webresource.axd"));
            Assert.IsFalse(_presenter.SingleSignOnAuthorizationRequired("install.aspx"));
            Assert.IsFalse(_presenter.SingleSignOnAuthorizationRequired("scriptresource.axd"));
            Assert.IsFalse(_presenter.SingleSignOnAuthorizationRequired("authentication.asmx/"));
            Assert.IsFalse(_presenter.SingleSignOnAuthorizationRequired("authentication.asmx/js"));
            Assert.IsFalse(_presenter.SingleSignOnAuthorizationRequired("authentication.asmx/jsdebug"));
            Assert.IsFalse(_presenter.SingleSignOnAuthorizationRequired("TinyMCEHandler.aspx"));
        }

        private void PrepareSitesAndLocales()
        {
            _domainA = new Site() { Domain = "siteA" };
            _domainB = new Site() { Domain = "siteB" };
            IList<Site> sites = new List<Site>() { _domainA, _domainB };
            _domainA.AliasDomains.Add("aliasA1");
            _domainA.AliasDomains.Add("aliasA2");
            _domainB.AliasDomains.Add("aliasB1");
            _domainB.AliasDomains.Add("aliasB2");
            SiteRepositoryMock.Setup(m => m.GetSites()).Returns(sites);
            _en = new Locale() { AliasDomain = "enAliasDomain" };
            _sv = new Locale();
            _domainA.Locales.Add(_en);
            _domainA.Locales.Add(_sv);
            IList<Locale> locales = new List<Locale>() { _en, _sv };
            LocaleRepositoryMock.Setup(m => m.GetLocales()).Returns(locales);
        }
        
        [TestMethod]
        public void GetSite_ExistingMainDomain_Success()
        {
            GeneralPrepare();
            PrepareSitesAndLocales();
            Assert.AreSame(_domainA, _presenter.GetSite("siteA"));
        }

        [TestMethod]
        public void GetSite_ExistingAliasDomain_Success()
        {
            GeneralPrepare();
            PrepareSitesAndLocales();
            Assert.AreSame(_domainB, _presenter.GetSite("aliasB2"));
        }
        
        [TestMethod]
        public void GetSite_UnknownDomain_ReturnNull()
        {
            GeneralPrepare();
            PrepareSitesAndLocales();
            Assert.IsNull(_presenter.GetSite("www.xx.com"));
        }
        
        [TestMethod]
        public void GetLocaleByAliasDomain_AliasExists_ReturnLocale()
        {
            GeneralPrepare();
            PrepareSitesAndLocales();
            Assert.AreSame(_en, _presenter.GetLocaleByAliasDomain("EnAliasDomain"));
        }
        
        [TestMethod]
        public void GetLocaleByAliasDomain_UnknownAlias_ReturnNull()
        {
            GeneralPrepare();
            PrepareSitesAndLocales();
            Assert.IsNull(_presenter.GetLocaleByAliasDomain("xxx"));
        }
        
        [TestMethod]
        public void GetMasterTemplate()
        {
            GeneralPrepare();
            _presenter.GetMasterTemplate(1);
            SiteRepositoryMock.Verify(m => m.GetMasterTemplate(1));
        }

        [TestMethod]
        public void GetDetailTemplate()
        {
            GeneralPrepare();
            _presenter.GetDetailTemplate(1);
            SiteRepositoryMock.Verify(m => m.GetDetailTemplate(1));
        }
        
        [TestMethod]
        public void GetLocale()
        {
            GeneralPrepare();
            _presenter.GetLocale(1);
            LocaleRepositoryMock.Verify(m => m.GetLocale(1));
        }

        [TestMethod]
        public void GetContentTranslationVersion()
        {
            GeneralPrepare();
            _presenter.GetContentTranslationVersion(1);
            ContentRepositoryMock.Verify(m => m.GetContentTranslationVersion(1));
        }
        
        [TestMethod]
        public void GetPageByID()
        {
            GeneralPrepare();
            _presenter.GetPage(1);
            PageRepositoryMock.Verify(m => m.GetPage(1));
        }
        
        [TestMethod]
        public void GetPage()
        {
            GeneralPrepare();

        }
        
        [TestMethod]
        public void GetMediaFile()
        {
            GeneralPrepare();

        }
    }
}
